import 'package:flutter/material.dart';
import 'package:aio/aio.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/http_data.dart';
import 'package:app/src/http/model/config_model.dart';
import 'package:app/src/manager/audit_manager.dart';
import 'package:app/src/manager/file_manager.dart';
import 'package:app/src/presenter/review_presenter.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/works/public/acom/ui_receipt_item_info.dart';
import 'package:app/src/ui/route/works/public/items/ui_attachment.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_contractInfo.dart';
import 'package:app/src/ui/route/works/public/items/ui_customersInfo.dart';
import 'package:app/src/ui/route/works/public/items/ui_new_customer.dart';
import 'package:app/src/ui/route/works/public/items/ui_node_list.dart';
import 'package:app/src/ui/route/works/public/items/ui_note.dart';
import 'package:app/src/ui/route/works/public/items/ui_order_info.dart';
import 'package:app/src/ui/route/works/public/acom/ui_order_item_info.dart';
import 'package:app/src/ui/route/works/public/items/ui_orderschangefactory.dart';
import 'package:app/src/ui/route/works/public/items/ui_payment.dart';
import 'package:app/src/ui/route/works/public/acom/ui_process_info.dart';
import 'package:app/src/ui/route/works/public/items/ui_process_list.dart';
import 'package:app/src/ui/route/works/public/acom/ui_product.dart';
import 'package:app/src/ui/route/works/public/items/ui_product_info.dart';
import 'package:app/src/ui/route/works/public/acom/ui_receipt_details_info.dart';
import 'package:app/src/ui/route/works/public/items/ui_receiptapply.dart';
import 'package:app/src/ui/route/works/public/acom/ui_shelft_process.dart';
import 'package:app/src/ui/route/works/public/items/ui_specialclauses.dart';
import 'package:app/src/ui/route/works/public/review_details_ui.dart';
import 'package:app/src/ui/route/works/public/review_submitted.dart';
import 'package:app/src/ui/route/works/public/review_submitted_export.dart';

///开票审核详情
class ReceiptApplyDetailsRoute extends StatefulWidget {
  ReceiptApplyDetailsRoute();

  @override
  _State createState() => _State();
}

class _State extends State<ReceiptApplyDetailsRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    ConfigModel.getFactorygetlist(context, false, (value) {
      if (value != null && value!.isSuccessForData()) {
        presenter.setStateBody?.call(() {
          presenter?.factories = value.Data;
        });
      }
    });
    presenter.getContractapplygetinfoWithDelay(context); //获取审核详情数据
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    FileManager.delAll(); //删除导出的文件。
  }

  @override
  Widget build(BuildContext context) {
    return ReviewDetailsUi.build(context, presenter, _body);
  }

  Widget _body(BuildContext context) {
    if (presenter?.mDatas == null) {
      return AppUi.getEmptyView();
    }
    var IsApproval = AuditManager.getAuditManager().IsApproval; //判断是否已审核
    List<Widget> children = [];
    children.add(UiReceiptapplyInfo.build(context, presenter?.mDatas?.Receipt,
        presenter?.mOldDatas?.Receipt, AuditManager.getAuditManager().IsApproval)); //开票信息
    children.add(UiNote.build(
        context,
        presenter?.mOldDatas?.Receipt?.ReceiptTitle2,
        presenter?.mDatas?.Receipt?.ReceiptTitle2,
        title: AppStrings.of(context).ReceiptTitle2)); //发票备注
    children.add(UiReceiptDetailsInfo.build(
        context, presenter?.mDatas?.ReceiptDetails)); //已开发票
    children.add(UiReceiptItemInfo.build(
        context, presenter?.mDatas?.ReceiptItemList)); //申请条目
    children.add(UiAttachment.build(context, presenter?.mDatas?.FileList)); //附件
    children.add(UiNoteList.build(context, presenter?.mDatas?.NodeList)); //审核日志
    Widget submit;
    if (IsApproval) {
      //已审核;导出单据
      submit = ReviewSubmittedWithExportStateful();
    } else {
      //未审核
      submit = ReviewSubmittedStateful(
        presenter?.mDatas,
        hasEnd: true,
      );
    }
    return Column(
      children: [
        Expanded(child: UiBase.listView(context, children)),
        Visibility(child: submit, visible: presenter?.mDatas != null)
      ],
    );
  }
}

class _Presenter extends ReviewPresenter {
  //工厂信息
  List<Factory>? factories = null;
}
